const axios = require('axios');
const { getWxAPIAccessToken } = require('./accesstoken');
const wxConfig = require('../../../config/wxapi.json');

async function sendTemplateMessage (openId, templateId, page, data) {
    const url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send';
    const res = await axios.post(url + '?access_token=' + await getWxAPIAccessToken(), {
        touser: openId,
        mp_template_msg: {
            template_id: templateId,
            appid: wxConfig.secret.accountAppId.trim(),
            url: 'http://weixin.qq.com/download',
            miniprogram: {
                appid: wxConfig.secret.appId.trim(),
                pagepath: page
            },
            data: data
        }
    });

    return res.data;
}

module.exports = {
    sendTemplateMessage
};
